package lily.phs.mental;

import lily.phs.object.VisitMentals;
import lily.phs.urh.R;
import lily.ui.control.MyPopupWindow;
import android.content.Context;
import android.text.InputType;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

public class VisitMentalsView_2 extends LinearLayout implements OnClickListener, OnFocusChangeListener{

	private Context mContext;
	private EditText et_last_leave_hos_date,et_lab_check,et_drug_reaction,et_transfer_reason,et_transfer_hospital,
	et_medicine_1,et_medicine_2,et_medicine_3,et_medi_1_usage,et_medi_2_usage,et_medi_3_usage,
	et_medi_1_dosage,et_medi_2_dosage,et_medi_3_dosage,et_recover_other,et_next_visit_time,et_doctor;
	private CheckBox cb_recover_1,cb_recover_2,cb_recover_3,cb_recover_4,cb_recover_5;
	private RadioGroup rg_hospital,rg_has_lab_check,rg_medi_comply,rg_has_drug_reaction,rg_treatment,
	rg_need_transfer,rg_classify;
	private RadioButton rb_hospital_0,rb_hospital_1,rb_hospital_2,rb_lab_check_1,rb_lab_check_2,
	rb_medi_comply_1,rb_medi_comply_2,rb_medi_comply_3,rb_drug_reaction_1,rb_drug_reaction_2,rb_treatment_1,
	rb_treatment_2,rb_treatment_3,rb_treatment_4,rb_need_transfer_1,rb_need_transfer_2,
	rb_classify_1,rb_classify_2,rb_classify_3,rb_classify_0;
	
	private MyPopupWindow mPopupWindow;
	
	private VisitMentals vm;
	private int profileId = 0;
	private int times = 0;
	private View child;
	
	public int getProfileId() {
		return profileId;
	}

	public void setProfileId(int profileId) {
		this.profileId = profileId;
	}

	public int getTimes() {
		return times;
	}

	public void setTimes(int times) {
		this.times = times;
	}

	public VisitMentalsView_2(Context context,int profileid,int times) {
		super(context);
		// TODO Auto-generated constructor stub
		this.mContext = context;
		this.profileId = profileid;
		this.times = times;
		
		final LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		child = inflater.inflate(R.layout.visit_record_for_mentals_2, null);
		
		
	}
	
	@Override
	public void onFocusChange(View v, boolean hasFocus) {
		// TODO Auto-generated method stub
		if(v==et_last_leave_hos_date&&hasFocus){
			mPopupWindow = new MyPopupWindow(mContext);			
			mPopupWindow.showAtLocation(v.findViewById(R.id.last_leave_hos_date), Gravity.CENTER, 0, 0);

			Button btn=mPopupWindow.getButton();
			btn.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					et_last_leave_hos_date.setText(mPopupWindow.getDate());
					mPopupWindow.dismiss();
				}
				
			});
		}
		else if(v==et_next_visit_time&&hasFocus){
			mPopupWindow = new MyPopupWindow(mContext);			
			mPopupWindow.showAtLocation(v.findViewById(R.id.next_visit_time), Gravity.CENTER, 0, 0);

			Button btn=mPopupWindow.getButton();
			btn.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					et_next_visit_time.setText(mPopupWindow.getDate());
					mPopupWindow.dismiss();
				}
				
			});
		}
		else if(v==et_lab_check&&hasFocus){
			rb_lab_check_2.setChecked(true);
		}
		else if(v==et_drug_reaction&&hasFocus){
			rb_drug_reaction_2.setChecked(true);
		}
		else if(v==et_recover_other&&hasFocus){
			cb_recover_5.setChecked(true);
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if(v==et_last_leave_hos_date){
			mPopupWindow = new MyPopupWindow(mContext);			
			mPopupWindow.showAtLocation(v.findViewById(R.id.last_leave_hos_date), Gravity.CENTER, 0, 0);

			Button btn=mPopupWindow.getButton();
			btn.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					et_last_leave_hos_date.setText(mPopupWindow.getDate());
					mPopupWindow.dismiss();
				}
				
			});
		}
		else if(v==et_next_visit_time){
			mPopupWindow = new MyPopupWindow(mContext);			
			mPopupWindow.showAtLocation(v.findViewById(R.id.next_visit_time), Gravity.CENTER, 0, 0);

			Button btn=mPopupWindow.getButton();
			btn.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					et_next_visit_time.setText(mPopupWindow.getDate());
					mPopupWindow.dismiss();
				}
				
			});
		}
		else if(v==et_lab_check){
			rb_lab_check_2.setChecked(true);
		}
		else if(v==et_drug_reaction){
			rb_drug_reaction_2.setChecked(true);
		}
		else if(v==et_recover_other){
			cb_recover_5.setChecked(true);
		}

	}

	/**
	 * 控件与xml文件关联
	 */
	public void findViewsById() {
		et_last_leave_hos_date = (EditText)child.findViewById(R.id.last_leave_hos_date);
		et_last_leave_hos_date.setInputType(InputType.TYPE_NULL);
		et_last_leave_hos_date.setOnClickListener(this);
		et_last_leave_hos_date.setOnFocusChangeListener(this);
		et_lab_check = (EditText)child.findViewById(R.id.lab_check);
		et_lab_check.setOnClickListener(this);
		et_lab_check.setOnFocusChangeListener(this);
		et_drug_reaction = (EditText)child.findViewById(R.id.drug_reaction);
		et_drug_reaction.setOnClickListener(this);
		et_drug_reaction.setOnFocusChangeListener(this);
		et_transfer_reason = (EditText)child.findViewById(R.id.transfer_reason);
		et_transfer_hospital = (EditText)child.findViewById(R.id.transfer_hospital);
		et_medicine_1 = (EditText)child.findViewById(R.id.medicine_1);
		et_medicine_2 = (EditText)child.findViewById(R.id.medicine_2);
		et_medicine_3 = (EditText)child.findViewById(R.id.medicine_3);
		et_medi_1_usage = (EditText)child.findViewById(R.id.medi_1_usage);
		et_medi_2_usage = (EditText)child.findViewById(R.id.medi_2_usage);
		et_medi_3_usage = (EditText)child.findViewById(R.id.medi_3_usage);
		et_medi_1_dosage = (EditText)child.findViewById(R.id.medi_1_dosage);
		et_medi_2_dosage = (EditText)child.findViewById(R.id.medi_2_dosage);
		et_medi_3_dosage = (EditText)child.findViewById(R.id.medi_3_dosage);
		et_recover_other = (EditText)child.findViewById(R.id.recover_other);
		et_recover_other.setOnClickListener(this);
		et_recover_other.setOnFocusChangeListener(this);
		et_next_visit_time = (EditText)child.findViewById(R.id.next_visit_time);
		et_recover_other.setInputType(InputType.TYPE_NULL);
		et_next_visit_time.setInputType(InputType.TYPE_NULL);
		et_next_visit_time.setOnClickListener(this);
		et_next_visit_time.setOnFocusChangeListener(this);
		et_doctor = (EditText)child.findViewById(R.id.doctor);
		cb_recover_1 = (CheckBox)child.findViewById(R.id.recover_1);
		cb_recover_2 = (CheckBox)child.findViewById(R.id.recover_2);
		cb_recover_3 = (CheckBox)child.findViewById(R.id.recover_3);
		cb_recover_4 = (CheckBox)child.findViewById(R.id.recover_4);
		cb_recover_5 = (CheckBox)child.findViewById(R.id.recover_5);
		rg_hospital = (RadioGroup)child.findViewById(R.id.hospital);
		rg_has_lab_check = (RadioGroup)child.findViewById(R.id.has_lab_check);
		rg_medi_comply = (RadioGroup)child.findViewById(R.id.medi_comply);
		rg_has_drug_reaction = (RadioGroup)child.findViewById(R.id.has_drug_reaction);
		rg_treatment = (RadioGroup)child.findViewById(R.id.treatment);
		rg_need_transfer = (RadioGroup)child.findViewById(R.id.need_transfer);
		rg_classify = (RadioGroup)child.findViewById(R.id.classify);
		rb_hospital_0 = (RadioButton)child.findViewById(R.id.hospital_0);
		rb_hospital_1 = (RadioButton)child.findViewById(R.id.hospital_1);
		rb_hospital_2 = (RadioButton)child.findViewById(R.id.hospital_2);
		rb_lab_check_1 = (RadioButton)child.findViewById(R.id.lab_check_1);
		rb_lab_check_2 = (RadioButton)child.findViewById(R.id.lab_check_2);
		rb_medi_comply_1 = (RadioButton)child.findViewById(R.id.medi_comply_1);
		rb_medi_comply_2 = (RadioButton)child.findViewById(R.id.medi_comply_2);
		rb_medi_comply_3 = (RadioButton)child.findViewById(R.id.medi_comply_3);
		rb_drug_reaction_1 = (RadioButton)child.findViewById(R.id.drug_reaction_1);
		rb_drug_reaction_2 = (RadioButton)child.findViewById(R.id.drug_reaction_2);
		rb_treatment_1 = (RadioButton)child.findViewById(R.id.treatment_1);
		rb_treatment_2 = (RadioButton)child.findViewById(R.id.treatment_2);
		rb_treatment_3 = (RadioButton)child.findViewById(R.id.treatment_3);
		rb_treatment_4 = (RadioButton)child.findViewById(R.id.treatment_4);
		rb_need_transfer_1 = (RadioButton)child.findViewById(R.id.need_transfer_1);
		rb_need_transfer_2 = (RadioButton)child.findViewById(R.id.need_transfer_2);
		rb_classify_1 = (RadioButton)child.findViewById(R.id.classify_1);
		rb_classify_2 = (RadioButton)child.findViewById(R.id.classify_2);
		rb_classify_3 = (RadioButton)child.findViewById(R.id.classify_3);
		rb_classify_0 = (RadioButton)child.findViewById(R.id.classify_0);
	}
	
	/**
	 * 初始化views
	 */
	public void initViews() {
		// TODO Auto-generated method stub
		initialState();
		
		String temp = "";
		int itemp = -1;
		float ftemp = -1f;
		
		itemp = vm.getVime_hospital();
		switch(itemp){
		case 0:{
			rg_hospital.check(0);
			rb_hospital_0.setChecked(true);
			break;
		}
		case 1:{
			rg_hospital.check(1);
			rb_hospital_1.setChecked(true);
			break;
		}
		case 2:{
			rg_hospital.check(2);
			rb_hospital_2.setChecked(true);
			break;
		}
		}
		
		temp = vm.getVime_last_leave_hos_date();
		if(temp.length()>0){
			et_last_leave_hos_date.setText(temp);
		}
		
		itemp = vm.getVime_has_lab_check();
		switch(itemp){
		case 1:{
			rg_has_lab_check.check(0);
			rb_lab_check_1.setChecked(true);
			break;
		}
		case 2:{
			rg_has_lab_check.check(1);
			rb_lab_check_2.setChecked(true);
			temp = vm.getVime_lab_check();
			if(temp.length()>0){
				et_lab_check.setText(temp);
			}
			break;
		}
		}
		itemp = vm.getVime_medi_comply();
		switch(itemp){
		case 1:{
			rg_medi_comply.check(0);
			rb_medi_comply_1.setChecked(true);
			break;
		}
		case 2:{
			rg_medi_comply.check(1);
			rb_medi_comply_2.setChecked(true);
			break;
		}
		case 3:{
			rg_medi_comply.check(2);
			rb_medi_comply_3.setChecked(true);
			break;
		}
		}
		itemp = vm.getVime_has_drug_reaction();
		switch(itemp){
		case 1:{
			rg_has_drug_reaction.check(0);
			rb_drug_reaction_1.setChecked(true);
			break;
		}
		case 2:{
			rg_has_drug_reaction.check(1);
			rb_drug_reaction_2.setChecked(true);
			temp = vm.getVime_drug_reaction();
			if(temp.length()>0){
				et_drug_reaction.setText(temp);
			}
			break;
		}
		}
		itemp = vm.getVime_treatment();
		switch(itemp){
		case 1:{
			rg_treatment.check(0);
			rb_treatment_1.setChecked(true);
			break;
		}
		case 2:{
			rg_treatment.check(1);
			rb_treatment_2.setChecked(true);
			break;
		}
		case 3:{
			rg_treatment.check(2);
			rb_treatment_3.setChecked(true);
			break;
		}
		case 4:{
			rg_treatment.check(3);
			rb_treatment_4.setChecked(true);
			break;
		}
		}
		itemp = vm.getVime_need_transfer();
		switch(itemp){
		case 1:{
			rg_need_transfer.check(0);
			rb_need_transfer_1.setChecked(true);
			break;
		}
		case 2:{
			rg_need_transfer.check(1);
			rb_need_transfer_2.setChecked(true);
			temp = vm.getVime_transfer_reason();
			if(temp.length()>0){
				et_transfer_reason.setText(temp);
			}
			temp = vm.getVime_transfer_hospital();
			if(temp.length()>0){
				et_transfer_hospital.setText(temp);
			}
			break;
		}
		}
		temp = vm.getVime_medicine_1();
		if(temp.length()>0){
			et_medicine_1.setText(temp);
			itemp = vm.getVime_medi_1_usage();
			if(itemp!=-1){
				et_medi_1_usage.setText(String.valueOf(itemp));
			}
			ftemp = vm.getVime_medi_1_dosage();
			if(ftemp!=-1f){
				et_medi_1_dosage.setText(String.valueOf(ftemp));
			}
		}
		temp = vm.getVime_medicine_2();
		if(temp.length()>0){
			et_medicine_2.setText(temp);
			itemp = vm.getVime_medi_2_usage();
			if(itemp!=-1){
				et_medi_2_usage.setText(String.valueOf(itemp));
			}
			ftemp = vm.getVime_medi_2_dosage();
			if(ftemp!=-1f){
				et_medi_2_dosage.setText(String.valueOf(ftemp));
			}
		}
		temp = vm.getVime_medicine_3();
		if(temp.length()>0){
			et_medicine_3.setText(temp);
			itemp = vm.getVime_medi_3_usage();
			if(itemp!=-1){
				et_medi_3_usage.setText(String.valueOf(itemp));
			}
			ftemp = vm.getVime_medi_3_dosage();
			if(ftemp!=-1f){
				et_medi_3_dosage.setText(String.valueOf(ftemp));
			}
		}
		
		temp = vm.getVime_recover();
		if(temp.length()>0){
			String[] ss = temp.split(",");
			for(int i=0;i<ss.length;i++){
				int k = Integer.parseInt(ss[i]);
				switch(k){
				case 1:{
					cb_recover_1.setChecked(true);
					break;
				}
				case 2:{
					cb_recover_2.setChecked(true);
					break;
				}
				case 3:{
					cb_recover_3.setChecked(true);
					break;
				}
				case 4:{
					cb_recover_4.setChecked(true);
					break;
				}
				case 5:{
					cb_recover_5.setChecked(true);
					temp = vm.getVime_recover_other();
					if(temp.length()>0){
						et_recover_other.setText(temp);
					}
					break;
				}
				}
			}
		}
		
		itemp = vm.getVime_classify();
		switch(itemp){
		case 1:{
			rg_classify.check(0);
			rb_classify_1.setChecked(true);
			break;
		}
		case 2:{
			rg_classify.check(1);
			rb_classify_2.setChecked(true);
			break;
		}
		case 3:{
			rg_classify.check(2);
			rb_classify_3.setChecked(true);
			break;
		}
		case 0:{
			rg_classify.check(3);
			rb_classify_0.setChecked(true);
			break;
		}
		}
		
		temp = vm.getVime_next_visit_time();
		if(temp.length()>0){
			et_next_visit_time.setText(temp);
		}
		temp = vm.getVime_doctor();
		if(temp.length()>0){
			et_doctor.setText(temp);
		}
		
	}
	
	/**
	 * 初始时状态：横线为空，所有选择项未选中
	 */
	private void initialState() {
		et_last_leave_hos_date.setText("");
		et_lab_check.setText("");
		et_drug_reaction.setText("");
		et_transfer_reason.setText("");
		et_transfer_hospital.setText("");
		et_medicine_1.setText("");
		et_medicine_2.setText("");
		et_medicine_3.setText("");
		et_medi_1_usage.setText("");
		et_medi_2_usage.setText("");
		et_medi_3_usage.setText("");
		et_medi_1_dosage.setText("");
		et_medi_2_dosage.setText("");
		et_medi_3_dosage.setText("");
		et_recover_other.setText("");
		et_next_visit_time.setText("");
		et_doctor.setText("");
		cb_recover_1.setChecked(false);
		cb_recover_2.setChecked(false);
		cb_recover_3.setChecked(false);
		cb_recover_4.setChecked(false);
		cb_recover_5.setChecked(false);
		rb_hospital_0.setChecked(false);
		rb_hospital_1.setChecked(false);
		rb_hospital_2.setChecked(false);
		rb_lab_check_1.setChecked(false);
		rb_lab_check_2.setChecked(false);
		rb_medi_comply_1.setChecked(false);
		rb_medi_comply_2.setChecked(false);
		rb_medi_comply_3.setChecked(false);
		rb_drug_reaction_1.setChecked(false);
		rb_drug_reaction_2.setChecked(false);
		rb_treatment_1.setChecked(false);
		rb_treatment_2.setChecked(false);
		rb_treatment_3.setChecked(false);
		rb_treatment_4.setChecked(false);
		rb_need_transfer_1.setChecked(false);
		rb_need_transfer_2.setChecked(false);
		rb_classify_1.setChecked(false);
		rb_classify_2.setChecked(false);
		rb_classify_3.setChecked(false);
		rb_classify_0.setChecked(false);
	}
	
	/**
	 * 从数据库中检出数据
	 */
	public void fetchData(){
		vm = new VisitMentals(mContext);
		vm.selectVisitMentals(profileId, times);
	}
	
	/**
	 * 将view添加到父view中
	 */
	public void setMyView(){
		this.removeAllViews();
		LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
		this.addView(child,lp);
	}
	
	/**
	 * 将整个界面作为一个view对象
	 * @return 整个界面view
	 */
	public View getMyView(){
		return child;
	}
	
	/**
	 * 保存views的值到数据库
	 */
	public void saveViews() {
		VisitMentals vms = new VisitMentals(mContext);
		vms.selectVisitMentals(profileId, times);
		
		String temp = "";
		StringBuffer sb;
		
		if(rb_hospital_0.isChecked()==true){
			vms.setVime_hospital(0);
		}else if(rb_hospital_1.isChecked()==true){
			vms.setVime_hospital(1);
		}else if(rb_hospital_2.isChecked()==true){
			vms.setVime_hospital(2);
		}
		temp = et_last_leave_hos_date.getText().toString();
		if(temp.length()>0){
			vms.setVime_last_leave_hos_date(temp);
		}
		
		if(rb_lab_check_1.isChecked()==true){
			vms.setVime_has_lab_check(1);
		}else if(rb_lab_check_2.isChecked()==true){
			vms.setVime_has_lab_check(2);
			temp = et_lab_check.getText().toString();
			if(temp.length()>0){
				vms.setVime_lab_check(temp);
			}
		}
		if(rb_medi_comply_1.isChecked()==true){
			vms.setVime_medi_comply(1);
		}else if(rb_medi_comply_2.isChecked()==true){
			vms.setVime_medi_comply(2);
		}else if(rb_medi_comply_3.isChecked()==true){
			vms.setVime_medi_comply(3);
		}
		if(rb_drug_reaction_1.isChecked()==true){
			vms.setVime_has_drug_reaction(1);
		}else if(rb_drug_reaction_2.isChecked()==true){
			vms.setVime_has_drug_reaction(2);
			temp = et_drug_reaction.getText().toString();
			if(temp.length()>0){
				vms.setVime_drug_reaction(temp);
			}
			
		}
		if(rb_treatment_1.isChecked()==true){
			vms.setVime_treatment(1);
		}else if(rb_treatment_2.isChecked()==true){
			vms.setVime_treatment(2);
		}else if(rb_treatment_3.isChecked()==true){
			vms.setVime_treatment(3);
		}else if(rb_treatment_4.isChecked()==true){
			vms.setVime_treatment(4);
		}
		
		if(rb_need_transfer_1.isChecked()==true){
			vms.setVime_need_transfer(1);
		}else if(rb_need_transfer_2.isChecked()==true){
			vms.setVime_need_transfer(2);
			temp = et_transfer_reason.getText().toString();
			if(temp.length()>0){
				vms.setVime_transfer_reason(temp);
			}
			temp = et_transfer_hospital.getText().toString();
			if(temp.length()>0){
				vms.setVime_transfer_hospital(temp);
			}
		}
		
		temp = et_medicine_1.getText().toString();
		if(temp.length()>0){
			vms.setVime_medicine_1(temp);
			temp = et_medi_1_usage.getText().toString();
			if(temp.length()>0){
				vms.setVime_medi_1_usage(Integer.parseInt(temp));
			}
			temp = et_medi_1_dosage.getText().toString();
			if(temp.length()>0){
				vms.setVime_medi_1_dosage(Float.parseFloat(temp));
			}
		}
		temp = et_medicine_2.getText().toString();
		if(temp.length()>0){
			vms.setVime_medicine_2(temp);
			temp = et_medi_2_usage.getText().toString();
			if(temp.length()>0){
				vms.setVime_medi_2_usage(Integer.parseInt(temp));
			}
			temp = et_medi_2_dosage.getText().toString();
			if(temp.length()>0){
				vms.setVime_medi_2_dosage(Float.parseFloat(temp));
			}
		}
		temp = et_medicine_3.getText().toString();
		if(temp.length()>0){
			vms.setVime_medicine_3(temp);
			temp = et_medi_3_usage.getText().toString();
			if(temp.length()>0){
				vms.setVime_medi_3_usage(Integer.parseInt(temp));
			}
			temp = et_medi_3_dosage.getText().toString();
			if(temp.length()>0){
				vms.setVime_medi_3_dosage(Float.parseFloat(temp));
			}
		}
		
		sb = new StringBuffer();
		if(cb_recover_1.isChecked()==true){
			sb.append("1,");
		}
		if(cb_recover_2.isChecked()==true){
			sb.append("2,");
		}
		if(cb_recover_3.isChecked()==true){
			sb.append("3,");
		}
		if(cb_recover_4.isChecked()==true){
			sb.append("4,");
		}
		if(cb_recover_5.isChecked()==true){
			sb.append("5,");
			temp = et_recover_other.getText().toString();
			if(temp.length()>0){
				vms.setVime_recover_other(temp);
			}
		}
		vms.setVime_recover(sb.toString());
		
		if(rb_classify_1.isChecked()==true){
			vms.setVime_classify(1);
		}else if(rb_classify_2.isChecked()==true){
			vms.setVime_classify(2);
		}else if(rb_classify_3.isChecked()==true){
			vms.setVime_classify(3);
		}else if(rb_classify_0.isChecked()==true){
			vms.setVime_classify(0);
		}
		
		temp = et_next_visit_time.getText().toString();
		if(temp.length()>0){
			vms.setVime_next_visit_time(temp);
		}
		temp = et_doctor.getText().toString();
		if(temp.length()>0){
			vms.setVime_doctor(temp);
		}
		
		vms.updateVisitMentals(profileId, times);
	}

}
